<?php


namespace common\models\service;


use common\helpers\commonApi;
use common\models\Order;

class StatService
{
    /**
     * 统计
     * @param $campus_id
     * @return array[]
     */
    public static function statistics($campus_id)
    {
        $day_start_time = date('Y-m-d 00:00:00');
        $day_end_time = date('Y-m-d 23:59:59');
        $yes_start_time = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
        $yes_end_time = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
        //订单
        $day_order = OrderService::deal($day_start_time,$day_end_time,$campus_id);
        $yes_order = OrderService::deal($yes_start_time,$yes_end_time,$campus_id);
        $order = OrderService::deal('','',$campus_id);
        //用户
        $day_num = UserService::num($day_start_time,$day_end_time);
        $yes_num = UserService::num($yes_start_time,$yes_end_time);
        $num = UserService::num('','');
        //校区
        $day_campus = CampusService::statistics($day_start_time,$day_end_time);
        $yes_campus = CampusService::statistics($yes_start_time,$yes_end_time);
        $campus = CampusService::statistics('','');
        //跑腿
        $day_running = RunningService::statistics($day_start_time,$day_end_time,$campus_id);
        $yes_running = RunningService::statistics($yes_start_time,$yes_end_time,$campus_id);
        $running = RunningService::statistics('','',$campus_id);
        //分销
        $day_dis = DisService::statistics($day_start_time,$day_end_time,$campus_id);
        $yes_dis = DisService::statistics($yes_start_time,$yes_end_time,$campus_id);
        $dis = DisService::statistics('','',$campus_id);
        $data = [
            'order_num'=>[
                'day'=>$day_order['num'],
                'yes'=>$yes_order['num'],
                'total'=>$order['num'],
                'diff'=>self::compare($day_order['num'],$yes_order['num']),
            ],
            'order_amount'=>[
                'day'=>$day_order['amount'],
                'yes'=>$yes_order['amount'],
                'total'=>$order['amount'],
                'diff'=>self::compare($day_order['amount'],$yes_order['amount']),
            ],
            'user_num'=>[
                'day'=>$day_num,
                'yes'=>$yes_num,
                'total'=>$num,
                'diff'=>self::compare($day_num,$yes_num),
            ],
            'campus'=>[
                'day'=>$day_campus,
                'yes'=>$yes_campus,
                'total'=>$campus,
                'diff'=>self::compare($day_campus,$yes_campus),
            ],
            'running_num'=>[
                'day'=>$day_running['num'],
                'yes'=>$yes_running['num'],
                'total'=>$running['num'],
                'diff'=>self::compare($day_running['num'],$yes_running['num']),
            ],
            'running_amount'=>[
                'day'=>$day_running['amount'],
                'yes'=>$yes_running['amount'],
                'total'=>$running['amount'],
                'diff'=>self::compare($day_running['amount'],$yes_running['amount']),
            ],
            'dis_num'=>[
                'day'=>$day_dis['num'],
                'yes'=>$yes_dis['num'],
                'total'=>$dis['num'],
                'diff'=>self::compare($day_dis['num'],$yes_dis['num']),
            ],
            'dis_amount'=>[
                'day'=>$day_dis['amount'],
                'yes'=>$yes_dis['amount'],
                'total'=>$dis['amount'],
                'diff'=>self::compare($day_dis['amount'],$yes_dis['amount']),
            ],
        ];
        return $data;
    }

    /**
     * 比较差异
     * @param $day
     * @param $yes
     * @return string
     */
    public static function compare($day,$yes)
    {
        $diff = $day-$yes;
        if ($diff>0){
            $html = "<span class='text-style' style='color: #FA2828'>+{$diff}</span>";
        }elseif ($diff==0){
            $html = "<span class='text-style' style='color: #FA2828'>+0</span>";
        }else{
            $html = "<span class='text-style' style='color: #009688'>-{$diff}</span>";
        }
        return $html;
    }

    /**
     * 销售额（支付金额）
     * @return array
     */
    public static function getTurnover($post)
    {
        $start_time = $post['start_time']??'';
        $end_time = $post['end_time']??'';
        if (!$start_time&&!$end_time){
            $start_time = date("Y-m-d 00:00:00",strtotime("-14 day"));
            $end_time = date("Y-m-d 23:59:59");
        }
        $xAxis = [];
        for ($i = strtotime($start_time); $i <= strtotime($end_time); $i = $i + 60 *60* 24) {
            $xAxis[] = date("Y-m-d",$i);
        }
        $data = Order::find()
            ->where(['pay_status'=>[1]])
            ->select(["FROM_UNIXTIME(UNIX_TIMESTAMP(create_time),'%Y-%m-%d') as time",'sum(pay_amount) as amount'])
            ->andFilterWhere(['between','create_time',$start_time,$end_time])
            ->groupBy(['time'])
            ->asArray()->all();
        $data = array_column($data,'amount','time');
        $series = [];
        foreach ($xAxis as &$axi){
            $series[] = $data[$axi]??0;
            $axi = date('m-d',strtotime($axi));
        }

        return commonApi::message('获取成功',true,['xAxis'=>$xAxis,'series'=>$series]);
    }
}